<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
				xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
  <meta charset="utf-8"/>
  <title>用户列表</title>
  <link rel="stylesheet" href="/css/layui.css" media="all"/>
  <link rel="stylesheet" href="/css/user.css" media="all" />
</head>
<body>
<div class="userDiv">
<br/>
  昵称：
  <div class="layui-inline">
    <input class="layui-input" name="nickname" id="nickname" autocomplete="off"/>
  </div>
  真实姓名：
  <div class="layui-inline">
    <input class="layui-input" name="realname" id="realname" autocomplete="off"/>
  </div>
 手机：
 <div class="layui-inline">
   <input class="layui-input" name="phone" id="phone" autocomplete="off"/>
 </div>
 邮箱：
  <div class="layui-inline">
    <input class="layui-input" name="email" id="email" autocomplete="off"/>
  </div>
 地址：
  <div class="layui-inline">
    <input class="layui-input" name="address" id="address" autocomplete="off"/>
  </div> 
  性别：  
  <div class="layui-inline">
   	<form class="layui-form" action="">
    <select name="sex" id="sex" autocomplete="off">
    		  <option value="">--请选择--</option>
              <option value="0">女</option>
              <option value="1">男</option>
    </select>
    </form>
  </div>
   
  <button class="layui-btn" data-type="reload">搜索</button>
</div>


<table class="layui-hide" id="userTable" lay-filter="tableFilter"></table>
 
<script src="/layui.js"></script>
<script>
/*<![CDATA[*/
layui.use('table', function(){
  var table = layui.table;
  
  //第一个实例
  table.render({
    elem: '#userTable'
    ,height: 'full'                   
    ,url: '/admin/getUser' //数据接口
    ,where:{jsonString:'{afterAuth:0}'}
    ,request: {
    		  pageName: 'begin' //页码的参数名称，默认：page
    		  ,limitName: 'length' //每页数据量的参数名，默认：limit
    		  }
    ,page: true //开启分页
    ,even: true
    ,cols: [[ //表头
      {field: 'username', title: '用户名', width:100, align:'center'}
      ,{field: 'nickname', title: '昵称', width:120, align:'center'}
      ,{field: 'realname', title: '真实姓名', width:85, align:'center'}
      ,{field: 'sexName', title: '性别', width:60, align:'center'}
      ,{field: 'address', title: '地址', width:130, align:'center'}
      ,{field: 'phone', title: '手机号', width:155, align:'center'}
      ,{field: 'email', title: '邮箱', width:180, align:'center'}
      ,{field: 'authProcess', title: '认证进程', width:140, align:'center'}
      ,{field: 'deposit', title: '押金（元）', sort: true, width:110, align:'center'}
      ,{field: 'level', title: '共享图书（本）', sort: true,width:135, align:'center'}
      ,{fixed: 'right', title: '操作',width:150, align:'center', toolbar: '#handler'} //这里的toolbar值是模板元素的选择器
    ]]
    ,id: 'userReload'
   	,done: function(res, page, count){
           //如果是异步请求数据方式，res即为你接口返回的信息。  
           //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度  
           
           $("[data-field='authProcess']").children().each(function(){
        	   if($(this).text()=='0'){
                   $(this).text("未认证");
               }
        	   if($(this).text()=='1'){
                   $(this).text("已实名");
               }
        	   if($(this).text()=='2'){
                   $(this).text("认证成功(押金)");
               }
        	   if($(this).text()=='3'){
                   $(this).text("认证成功(共享)");
               }
           })
   } 
  });
    
    //条件查询
	var $ = layui.$, active = {
		reload : function() {
			var nickname = $('#nickname').val().trim();
			var realname = $('#realname').val().trim();
			var phone = $('#phone').val().trim();
			var email = $('#email').val().trim();
			var address = $('#address').val().trim();
			var sex = $('#sex').val().trim();
			var jsonString = "";
			
			var i = 0;
			jsonString = "{";
			if(nickname!=null&&nickname!=""){
				i++;
				jsonString = jsonString + "nickname:'" + nickname + "'";
			}
			if(realname!=null&&realname!=""){
				if(i!=0){
					jsonString = jsonString + ","
				}
				i++;
				jsonString = jsonString + "realname:'" + realname  + "'";
			}
			if(phone!=null&&phone!=""){
				if(i!=0){
					jsonString = jsonString + ","
				}
				i++;
				jsonString = jsonString + "phone:'" + phone  + "'";
			}
			if(email!=null&&email!=""){
				if(i!=0){
					jsonString = jsonString + ","
				}
				i++;
				jsonString = jsonString + "email:'" + email  + "'";
			}
			if(address!=null&&address!=""){
				if(i!=0){
					jsonString = jsonString + ","
				}
				i++;
				jsonString = jsonString + "address:'" + address  + "'";
			}
			if(sex!=null&&sex!=""){
				if(i!=0){
					jsonString = jsonString + ","
				}
				i++;
				jsonString = jsonString + "sex:'" + sex  + "'";
			}
			jsonString = jsonString + "}"
			
			
			//执行重载
			table.reload('userReload', {
				page : {
					curr : 1
				//重新从第 1 页开始
				},
				where : {
						jsonString : jsonString
				}
			});
		}
	};

	$('.userDiv .layui-btn').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	
	
	//操作绑定
	table.on('tool(tableFilter)', function(obj){ //注：tool是固定的工具条事件名，tableFilter是table原始容器的属性 lay-filter="对应的值"
		  var data = obj.data; //获得当前行数据
		  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		  var tr = obj.tr; //获得当前行 tr 的DOM对象
		 
		  if(layEvent === 'deposit'){
			  var openIndex = layer.open({
			        type: 1,
			        title: '押金缴纳--'+data.realname,
			        area : ['350px' , '200px'], 
			        scrollbar: false,//禁止浏览器滚动
			        closeBtn: 2,
			        content: $("#addDeposit"),
			        btn: ['确认缴纳', '取消'],
			        yes: function(index,layero){
			        	layero.find('button[lay-filter=subAddDeposit]').click();
			        },
			        cencel: function(){
			          
			        }
		        });
			  
				//提交按钮
			    layui.use(['form', 'jquery'], function(){
				  var form = layui.form;
				  $=layui.jquery;
				  form.on('submit(subAddDeposit)', function(fdata){
					  if(data.deposit==null){
						  data.deposit = 0;
					  }
					  var deposit = parseInt(fdata.field.deposit) + parseInt(data.deposit);
					  if(data.authProcess!=3){
						  if(deposit>0){
							  data.authProcess = 2;
						  }else{
							  data.authProcess = 1;
						  }
					  }
					  var userId = data.id;
					  $.ajax({
					    	url:"/admin/deposit",
					    	data:{deposit:deposit,id:userId,authProcess:data.authProcess},
					    	type:"POST",
					    	success:function(rdata){
					    		if(data.deposit<=0||deposit<=0){
					    			$(".layui-laypage-btn").click();
					    		}else{
					    		obj.update({
					    			deposit:deposit,
					    			});
					    		}
							    layer.msg('操作成功');
					    		layer.close(openIndex);
					    		$("#deposit-text").val('');
					    		form.render();
					    	},
					    	error:function(rdata){
					    		layer.alert('请重试..', {
								      title: '错误提示'
								    });
					    	}
					    });
					    return false;
				  });
				});
		  } else if(layEvent === 'share'){
			  var openIndex;
			  var msgIndex = layer.msg('该图书是否书库已拥有？', {
			        time: 20000,
			        closeBtn: 2,
			        btn: ['是', '否'],
				    yes: function(index,layero){
				    	layer.close(msgIndex);
				    	openIndex = layer.open({
					        type: 1,
					        title: '共享图书--'+data.realname,
					        area : ['400px' , '250px'], 
					        scrollbar: false,
					        closeBtn: 2,
					        content: $("#addOldBook"),
					        btn: ['确认共享', '取消'],
					        yes: function(index,layero){
					        	layero.find('button[lay-filter=subAddOldBook]').click();
					        },
					        cencel: function(){
					        }
				        });
			        },
			        btn2: function(){
			        	layer.msg("请到图书列表添加图书！");
			        }
			  });
			  
				//提交按钮
			    layui.use(['form', 'jquery'], function(){
				  var form = layui.form;
				  $=layui.jquery;
				  
				  form.on('submit(subAddOldBook)', function(fdata){
					  if(fdata.field.bookamount!=0){
						  var state;
						  var amount;
						  if(fdata.field.bookamount>0){
							  state="1";//共享
							  amount=fdata.field.bookamount;
						  }else{
							  state="0";//取回
							  amount=-fdata.field.bookamount;
						  }
						  if(data.level==null){
							  data.level=0;
						  }
						  if(data.deposit==null){
							  data.deposit=0;
						  }
						  var level = parseInt(fdata.field.bookamount) + parseInt(data.level);
						  $.ajax({
						    	url:"/admin/share",
						    	data:{userId:data.id,level:level,bookNumber:fdata.field.booknumber,
						    		amount:amount,state:state,deposit:data.deposit},
						    	type:"POST",
						    	success:function(rdata){
						    		if(rdata.rspCode=="000000"){
						    			$(".layui-laypage-btn").click();
									    layer.msg('操作成功');
							    		layer.close(openIndex);
							    		$("#form-booknumber").val("");
							    		$("#form-bookamount").val("");
						    		}else if (rdata.rspCode=="000002"){
						    			layer.msg('用户共享该书仅有'+rdata.data+'本');
						    		}else if (rdata.rspCode=="000003"){
						    			layer.msg(rdata.rspMsg);
						    		}else{
						    			layer.msg('操作失败，请重试');
						    		}
						    	},
						    	error:function(rdata){
						    		layer.alert('请重试..', {
									      title: '错误提示'
									    });
						    	}
						    }); 
					  }else{
						  layer.msg('请输入正确的数量');
					  }
					    return false;
				  });//subAddOldBook end
				  
				});//form end
			  
			  
		  }//share end
		});
	
});
/*]]>*/
</script>


<div class="form-group" id="addDeposit" hidden="hidden">
<form class="layui-form" action="">
	  <br/>	<br/>
	  <div class="layui-form-item">
	    <label class="layui-form-label">押金数</label>
	    <div class="layui-input-inline">
			<input type='text'  id='deposit-text' name='deposit'  required='required'  lay-verify='required|number'   class='layui-input' />	    </div>
	  </div>
	  <button class="layui-btn layui-hide" lay-submit="" lay-filter="subAddDeposit">确认添加</button>
</form>
</div>


<div class="form-group" id="addOldBook" hidden="hidden">
<form class="layui-form" action="">
	  <br/>	<br/>
	  <div class="layui-form-item">
	    <label class="layui-form-label">编号</label>
	    <div class="layui-input-inline">
	      <input type="text"  id="form-booknumber" name="booknumber" placeholder="请输入书库对应的编号" required="required"  lay-verify="required"  autocomplete="off" class="layui-input" />
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label">数量</label>
	    <div class="layui-input-inline">
	      <input type="text"  id="form-bookamount" name="bookamount" placeholder="请输入共享数量" required="required"  lay-verify="required|number"  autocomplete="off" class="layui-input" />
	    </div>
	  </div>
	  <button class="layui-btn layui-hide" lay-submit="" lay-filter="subAddOldBook">确认添加</button>
</form>
</div>



</body>

<script type="text/html" id="handler">
  <a class="layui-btn layui-btn-xs" lay-event="deposit">押金缴纳</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="share">图书共享</a>
</script>
</html>